<template>
  <el-dialog
    class="base-dialog"
    :class="{ readonly: readOnly }"
    :custom-class="'panna-dialog_small'"
    :title="title"
    :visible.sync="dialogFormVisible"
    :append-to-body="true"
    align="center"
    :before-close="handleClose"
    :close="handleClose"
  >
    <el-form ref="formData" :model="formData" status-icon>
      <el-form-item
        label="检测费用"
        :label-width="formLabelWidth"
        prop="detectionPrice"
      >
        <span>{{ stringPriceFormat(formData.detectionPrice) }}</span>
      </el-form-item>
      <el-form-item
        label="折扣方式"
        :label-width="formLabelWidth"
        prop="discountTypeCode"
      >
        <el-radio
          v-model="formData.discountTypeCode"
          :label="1"
          :disabled="readOnly"
          @change="chioseDiscount"
          >协议价格</el-radio
        >
        <el-radio
          v-model="formData.discountTypeCode"
          :label="2"
          :disabled="readOnly"
          @change="chioseDiscount"
          >整单折扣</el-radio
        >
      </el-form-item>

      <el-form-item
        label="折扣设置"
        :label-width="formLabelWidth"
        prop="discountTypeCode"
      >
        <el-button
          v-if="formData.discountTypeCode === 1"
          type="primary"
          style="width: 50%"
          @click="choiceAgreementPrice"
          >{{ choiceDetectionPricTitle }}</el-button
        >
        <div v-if="formData.discountTypeCode === 2">
          折扣率:
          <el-input-number
            v-model="formData.discount"
            style="width: 31%"
            :controls="false"
            :precision="2"
            :min="0"
            :max="100000000"
            @change="discountChange"
          ></el-input-number>
          <span>%</span>
        </div>
      </el-form-item>

      <el-form-item
        label="优惠价"
        :label-width="formLabelWidth"
        prop="favorablePrice"
      >
        <el-input-number
          style="width: 100%"
          :value="stringPriceFormat(formData.favorablePrice)"
          :controls="false"
          autocomplete="off"
          :precision="2"
          :min="0"
          :max="100000000"
          @input="valueChange"
        ></el-input-number>
      </el-form-item>
      <el-form-item
        label="最终价"
        :label-width="formLabelWidth"
        prop="finalPrice"
      >
        <span>{{ numFilter(stringPriceFormat(formData.finalPrice)) }}</span>
      </el-form-item>
    </el-form>

    <span v-if="!readOnly" slot="footer" class="base-dialog-footer">
      <el-button
        v-rights="'Update'"
        v-loading="submitLoad"
        type="primary"
        @click="submitForm(10)"
        >保 存</el-button
      >
      <el-button @click="handleClose">取 消</el-button>
    </span>
    <ChoiceAgreementPriceDialog
      ref="ChoiceAgreementPriceDialog"
      v-model="agreementPriceVisible"
      :read-only-detection-item-price.sync="readOnlyDetectionItemPrice"
      :agreement-price-dialog-table-data.sync="agreementPriceDialogTableData"
      :entrust-order-id.sync="entrustOrderId"
      :detection-item-data.sync="detectionItemData"
      @closeDialog="closeDialog"
    />
  </el-dialog>
</template>
<script>
import { stringPriceFormat, stringPriceCompute } from "@/utils/StringFormat";
import ChoiceAgreementPriceDialog from "./EntrustOrderPrice/ChoiceAgreementPriceDialog.vue";
import {
  update,
  get,
  CalculateEntrustPrice,
} from "./api/EntrustOrderPrice/EntrustOrderPrice";
export default {
  name: "AuditEntrustOrderPriceDialog",
  components: { ChoiceAgreementPriceDialog },
  props: {
    entrustOrderId: {
      type: String,
      default: "",
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    editId: {
      type: String,
      default: "",
    },
    fromSource: {
      type: String,
      default: "",
    },
    dataGroup: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      readOnlyDetectionItemPrice: false,
      submitLoad: false,
      choicePriceReadOnly: true,
      discountReadOnly: true,
      title: "",
      gridData: [{}],
      dialogVisible: false,
      agreementPriceVisible: false,
      agreementPriceDialogTableData: [],
      radio: "",
      choiceDetectionPricTitle: "",
      formData: {
        id: "",
        customerId: 0,
        entrustOrderId: "",
        detectionPrice: "",
        discountTypeCode: "",
        agreementPrice: "",
        discount: "",
        favorablePrice: "",
        finalPrice: "",
        pricelStatusCode: 1,
        entrustOrderDetectionItemPriceDtos: [],
      },
      tableData: [],
      requestArry: [],
      formLabelWidth: "100px",
      // 存放查询出来的检测项目价格数据
      detectionItemData: [],
    };
  },

  created() {
    if (this.fromSource === "PriceVerify") {
      this.init();
    }
  },
  methods: {
    // 截取当前数据到小数点后两位
    numFilter(value) {
      const realVal = parseFloat(value).toFixed(2);
      return realVal;
    },
    stringPriceFormat,
    valueChange(e) {
      this.formData.favorablePrice = e * 100;
      this.formData.finalPrice = stringPriceCompute(
        this.formData.detectionPrice,
        this.formData.favorablePrice,
        "-"
      );
    },
    // 更改折扣率触发事件
    discountChange() {
      this.formData.finalPrice = stringPriceCompute(
        this.formData.detectionPrice,
        parseFloat(this.formData.detectionPrice) *
        (parseFloat(this.formData.discount) / 100),
        "-"
      );
      this.formData.favorablePrice = stringPriceCompute(
        this.formData.detectionPrice,
        this.formData.finalPrice,
        "-"
      );
    },
    // 关闭弹框
    closeDialog(data) {
      this.detectionItemData = [];
      this.agreementPriceDialogTableData = data;
      this.formData.entrustOrderDetectionItemPriceDtos = [];
      var price = 0;
      for (let i = 0; i < this.agreementPriceDialogTableData.length; i++) {
        this.formData.entrustOrderDetectionItemPriceDtos.push({
          entrustOrderPriceId: this.entrustOrderId,
          detectionItemId: this.agreementPriceDialogTableData[i].id,
          detectionItemPriceCode:
            this.agreementPriceDialogTableData[i].agreementPriceState,
        });
        if (
          parseInt(
            this.agreementPriceDialogTableData[i].agreementPriceState
          ) === 1
        ) {
          price = stringPriceCompute(
            price,
            this.agreementPriceDialogTableData[i].firstAgreementPrice,
            "+"
          );
        } else if (
          parseInt(
            this.agreementPriceDialogTableData[i].agreementPriceState
          ) === 2
        ) {
          price = stringPriceCompute(
            price,
            this.agreementPriceDialogTableData[i].secondAgreementPrice,
            "+"
          );
        } else if (
          parseInt(
            this.agreementPriceDialogTableData[i].agreementPriceState
          ) === 3
        ) {
          price = stringPriceCompute(
            price,
            this.agreementPriceDialogTableData[i].thirdAgreementPrice,
            "+"
          );
        } else {
          price = stringPriceCompute(
            price,
            this.agreementPriceDialogTableData[i].agreementPrice,
            "+"
          );
        }
      }

      this.formData.favorablePrice = stringPriceCompute(
        this.formData.detectionPrice,
        price,
        "-"
      );
      this.formData.finalPrice = price;
    },
    init() {
      this.title = "价格核算";
      if (this.readOnly) {
        this.choiceDetectionPricTitle = "查看协议价";
        this.readOnlyDetectionItemPrice = true;
      } else {
        this.choiceDetectionPricTitle = "选择协议价";
        this.readOnlyDetectionItemPrice = false;
      }
      // 修改查询数据
      if (this.editId) {
        get({ Id: this.editId }).then((res) => {
          this.formData = res.data;
          this.choiceAgreementPriceButtonState();
        });
      } else {
        // 查询委托单总价格
        CalculateEntrustPrice({ Id: this.entrustOrderId }).then((res) => {
          this.formData.detectionPrice = res.data;
        });
      }
    },
    // 选择协议价按钮可用状态
    choiceAgreementPriceButtonState() {
      if (parseInt(this.formData.discountTypeCode) === 1) {
        this.choicePriceReadOnly = false;
        this.discountReadOnly = true;
      } else if (parseInt(this.formData.discountTypeCode) === 2) {
        this.discountReadOnly = false;
        this.choicePriceReadOnly = true;
      }
    },
    // 选择协议价
    choiceAgreementPrice() {
      this.detectionItemData = this.formData.entrustOrderDetectionItemPriceDtos;
      this.entrustOrderId = this.formData.entrustOrderId;
      this.agreementPriceVisible = true;
    },
    // 选择折扣
    chioseDiscount() {
      this.formData.finalPrice = 0;
      this.formData.favorablePrice = 0;
      this.choiceAgreementPriceButtonState();
      this.discountChange();
    },
    // 保存
    submitForm() {
      // 如果是整单折扣就清空存放协议价的entrustOrderDetectionItemPriceDtos
      if (this.formData.discountTypeCode === 2) {
        this.formData.entrustOrderDetectionItemPriceDtos = [];
      }
      this.formData.pricelStatusCode = 2;
      this.formData.dataGroup = this.dataGroup;
      update(this.formData).then((res) => {
        this.handleClose();
        // 刷新当前界面数据
        this.$emit("getList");
        this.$emit("close");
      });
    },
    setPrice() {
      this.$confirm(
        "批量设置价格将清空原有价格设置，请确认是否继续本操作?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
          callback: (action) => {
            if (action === "confirm") {
              return;
            }
          },
        }
      );
    },
    handleClose() {
      this.$refs.ChoiceAgreementPriceDialog.tableData = [];
      this.$refs.formData.resetFields();
      this.$emit("update:dialogFormVisible", false);
      this.$emit("closePriceDialog");
    },
  },
};
</script>
<style lang="scss" scoped>
.title {
  font-weight: bold;
  font-size: 25px;
}
.content {
  margin-top: 8px;
}
span {
  white-space: pre;
}
</style>
